package com.example.videomedian;

import android.app.ActivityOptions;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.text.Format;
import java.text.SimpleDateFormat;

public class VideoFuPing extends AppCompatActivity {

    private VideoView fuvideoView;
    int pos  ;
    private Button fureturun;
    private Button fuother;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_fu_ping);

        fuvideoView = findViewById(R.id.FuvideoView);

        Intent intent = getIntent();


       pos = intent.getIntExtra("play",0);

        play(pos);


fuvideoView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
              fureturun.setVisibility(View.VISIBLE);
              fuother.setVisibility(View.VISIBLE);
    }
});

        fureturun = findViewById(R.id.Fureturun);
        fureturun.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onClick(View view) {
                getZhuPIng(FormatTime(fuvideoView.getCurrentPosition()),pos);
              finish();
            }
        });

        fuother = findViewById(R.id.Fuother);
        fuother.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onClick(View view) {

             getZhuPIng(FormatTime(fuvideoView.getCurrentPosition()),pos);



                fuother.setVisibility(View.GONE);
                fureturun.setVisibility(View.GONE);
                String[]title=new String[VideoActivity.list.size()];

                for (int i = 0; i < VideoActivity.list.size(); i++) {

                    title[i]= VideoActivity.list.get(i).vName;
                }
                final AlertDialog.Builder builder=new AlertDialog.Builder(VideoFuPing.this);

                builder.setTitle("请选择App:");
                builder.setItems(title, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {


                     play(i);
                    }
                });
                builder.show();
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void getZhuPIng(String time,int pos) {

        DisplayManager displayManager = (DisplayManager) VideoFuPing.this.getSystemService(Context.DISPLAY_SERVICE);

        Display[] displays = displayManager.getDisplays();
        if (displays.length > 1) {

            Intent intent = new Intent(VideoFuPing.this, VideoActivity.class);
            intent.putExtra("time", time);
            intent.putExtra("pos",pos);
            intent.setClassName(getPackageName(), getPackageName() + ".VideoActivity");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

            ActivityOptions options = null;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                options = ActivityOptions.makeBasic();
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                options.setLaunchDisplayId(displays[0].getDisplayId());


            }

            startActivity(intent, options.toBundle());

        }
    }

    private String FormatTime(int currentPosition) {

        SimpleDateFormat format=new SimpleDateFormat("HH:mm:ss");
        String time=format.format(currentPosition);
        return time;
    }



    private void play(int pos) {

        this.pos=pos;
        fuvideoView.setVideoPath(VideoActivity.list.get(pos).path);

        MediaController mediaController=new MediaController(VideoFuPing.this);
        fuvideoView.setMediaController(mediaController);

        fuvideoView.start();
    }

}